/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kimstore.gui.stocksarrival;

import com.kimstore.main.controller.StocksArrivalTabController;
import com.kimstore.bean.Batch;
import com.kimstore.bean.Item;
import com.kimstore.bean.ItemStatus;
import com.kimstore.factory.DaoFactory;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

/**
 *
 * @author Administrator
 */
public class NewStocksController {

    private StocksArrivalTabController parentController;
    private NewStocksWindow window;
    private Batch batch = new Batch();
    private ArrayList<Item> items = new ArrayList<Item>();

    public NewStocksController(StocksArrivalTabController parentController) {
        this.parentController = parentController;
    }

    public ArrayList<Item> getItems(){
        return items;
    }
    
    public void displayWindow() {
        window = new NewStocksWindow(this);
                
        window.populateStaffComboBox(DaoFactory.getInstance().getStaffDao().findAll());
        window.displayWindow();
    }

    public void fromWindow(String batchNumber, String staff) {
        Date today = Calendar.getInstance().getTime();

        batch.setNumber(Long.parseLong(batchNumber));
        batch.setDateReceived(today);
        batch.setItems(items);
        batch.setReceivedBy(DaoFactory.getInstance().getStaffDao().findByName(staff));

        for (Item i : items) {
            ItemStatus history = new ItemStatus();
            
            history.setDate(today);
            history.setType(ItemStatus.RECEIVED);
            history.setRemark("Item has been received.");
            
            i.addHistory(history);
            
            i.setLatestStatus(ItemStatus.AVAILABLE);
            //DaoFactory.getInstance().getItemDao().saveOrUpdate(i);
        }
        
        DaoFactory.getInstance().getBatchDao().saveOrUpdate(batch);
        
        parentController.createNewStocksControllerCallback(batch, items);
    }

    public void createAddItemToStocksController() {
        AddItemToStocksController addItemToStocksController = new AddItemToStocksController(this);
        addItemToStocksController.displayWindow();
    }

    public void createAddItemToStocksControllerCallback(Item item) {
        items.add(item);

        window.addItemToTable(item);
    }

    void removeItem(String sku) {
        for(Item item: items) {
            if(item.getSerial().equals(sku)) {
                items.remove(item);
                break;
            }
        }
    }
    
    public void validate(String number) throws Error {
        if(items == null || items.size() < 1)
            throw new Error("<html>No <b>Item</b> has been added</html>");
        
        boolean exists = DaoFactory.getInstance().getBatchDao().existsNumber(Long.parseLong(number));
       
        if(exists) {
            throw new Error("<html><b>Batch Number</b> has already been taken. Press <br>the <b>Refresh</b> button to generate a new one</html>");
        }
    }
    
}
